package com.ruoyi.bussine.service.integration.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.bussine.common.enums.integration.PathEnum;
import com.ruoyi.bussine.service.common.ModuleConfigService;
import com.ruoyi.bussine.service.integration.AddressService;
import com.ruoyi.bussine.utils.ModuleHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengyz
 * @date 2025/3/13
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private ModuleHttpUtil moduleHttpUtil;

    @Autowired
    private ModuleConfigService moduleConfigService;

    @Override
    public JSONObject getAddressByLatLng(String latitude, String longitude) {
        return aMapApi(latitude, longitude);
    }

    /**
     * 高德地图，根据经纬度获取地址信息
     *
     * @param latitude
     * @param longitude
     * @return
     */
    private JSONObject aMapApi(String latitude, String longitude) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("output", "json");
        // 放到数据库配置里
        String aMapKey = moduleConfigService.getaMapKey();
        paramMap.put("key", aMapKey);
        paramMap.put("location", longitude + "," + latitude);
        String result = moduleHttpUtil.get(PathEnum.AMAP_ADDRESS_BY_LATLNG, paramMap);
        return JSONUtil.toBean(result, JSONObject.class);
    }

}
